/**
 * 
 */
package be.redo.dao;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import be.redo.model.entities.AirlineCompany;
import be.redo.model.entities.Flight;
import be.redo.model.entities.Line;

/**
 * @author Frank
 * 
 */
@Repository
public class FlightDAOImpl implements FlightDAO {
	private EntityManager entityManager;

	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	/**
	 * @see be.redo.dao.FlightDAO#create(be.redo.model.entities.Flight)
	 */
	@Override
	public void create(Flight instance) {
		try {
			entityManager.persist(instance);
		} catch (RuntimeException ex) {
			entityManager.clear();
			throw ex;
		}
	}

	/**
	 * @see be.redo.dao.FlightDAO#read(long)
	 */
	@Override
	public Flight read(long id) {
		return entityManager.find(Flight.class, id);
	}

	/**
	 * @see be.redo.dao.FlightDAO#update(be.redo.model.entities.Flight)
	 */
	@Override
	public void update(Flight instance) {
		try {
			entityManager.merge(instance);
			entityManager.flush();
		} catch (RuntimeException ex) {
			entityManager.clear();
			throw ex;
		}
	}

	/**
	 * @see be.redo.dao.FlightDAO#delete(long)
	 */
	@Override
	public void delete(long id) {
		Flight instance = entityManager.find(Flight.class, id);
		if (instance != null) {
			entityManager.remove(instance);
			entityManager.flush();
		}
	}

	/**
	 * @see be.redo.dao.FlightDAO#findAll()
	 */
	@Override
	public List<Flight> findAll() {
		TypedQuery<Flight> query = entityManager.createNamedQuery(
				"findAllFlights", Flight.class);
		return query.getResultList();
	}

	/**
	 * @see be.redo.dao.FlightDAO#count()
	 */
	@Override
	public int count() {
		TypedQuery<Number> query = entityManager.createNamedQuery(
				"findAantalFlights", Number.class);
		return (query.getSingleResult()).intValue();
	}

	/**
	 * @see be.redo.finders.FlightFinders#findFlightsByAirlineCompany(be.redo.model.entities.AirlineCompany)
	 */
	@Override
	public List<Flight> findFlightsByAirlineCompany(
			AirlineCompany airlineCompany) {
		TypedQuery<Flight> query = entityManager.createNamedQuery(
				"findFlightsByAirlineCompany", Flight.class);
		query.setParameter("AirlineCompany", airlineCompany);
		return query.getResultList();
	}

	/**
	 * @see be.redo.finders.FlightFinders#findFlightsByDepartureTimeBetween(java.util.Date,
	 *      java.util.Date)
	 */
	@Override
	public List<Flight> findFlightsByDepartureTimeBetween(Date lowerTime,
			Date higherTime) {
		TypedQuery<Flight> query = entityManager.createNamedQuery(
				"findFlightsByDepartureTimeBetween", Flight.class);
		query.setParameter("lowerTime", lowerTime);
		query.setParameter("higherTime", higherTime);
		return query.getResultList();
	}

	/**
	 * @see be.redo.finders.FlightFinders#findFlightsByDepartureTimeEquals(java.util.Date)
	 */
	@Override
	public List<Flight> findFlightsByDepartureTimeEquals(Date departureTime) {
		TypedQuery<Flight> query = entityManager.createNamedQuery(
				"findFlightsByDepartureTimeEquals", Flight.class);
		query.setParameter("departureTime", departureTime);
		return query.getResultList();
	}

	/**
	 * @see be.redo.finders.FlightFinders#findFlightsByLine(be.redo.model.entities.Line)
	 */
	@Override
	public List<Flight> findFlightsByLine(Line line) {
		TypedQuery<Flight> query = entityManager.createNamedQuery(
				"findFlightsByLine", Flight.class);
		query.setParameter("line", line);
		return query.getResultList();
	}

	/**
	 * @see be.redo.finders.FlightFinders#findFlightsByFlightCodeLike(java.lang.String)
	 */
	@Override
	public List<Flight> findFlightsByFlightCodeLike(String flightCode) {
		TypedQuery<Flight> query = entityManager.createNamedQuery(
				"findFlightsByFlightCodeLike", Flight.class);
		query.setParameter("flightCode", flightCode);
		return query.getResultList();
	}

	/**
	 * @see be.redo.finders.FlightFinders#findFlightsByLineWhereDepartureIsLaterThan(be.redo.model.entities.Line,
	 *      java.util.Date)
	 */
	@Override
	public List<Flight> findFlightsByLineWhereDepartureIsLaterThan(Line line,
			Date departureTime) {
		TypedQuery<Flight> query = entityManager.createNamedQuery(
				"findFlightsByLineWhereDepartureIsLaterThan", Flight.class);
		query.setParameter("line", line);
		query.setParameter("departureTime", departureTime);
		return query.getResultList();
	}

}
